//作业内容
public class ThreadDemo1 {
    public static  volatile int i=1;
    public  static void ThreadDemo2() throws InterruptedException {
        Object lock=new Object();
        Object lock1=new Object();
        Object lock2=new Object();
        Thread t=new Thread(()->{
            System.out.print("第"+i+"次打印: ");
            synchronized (lock2) {
                lock2.notify();
                i++;
            }
        });
        Thread t1=new Thread(()->{
            synchronized (lock2){
                try {
                    lock2.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.print("A"+" ");
            synchronized (lock){
                lock.notify();
            }
        });
        Thread t2=new Thread(()->{
            synchronized (lock){
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.print("B"+" ");
            synchronized (lock1){
                lock1.notify();
            }
        });
        Thread t3=new Thread(()->{
            synchronized (lock1){
                try {
                    lock1.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.print("C"+" ");
        });
        t2.start();
        t3.start();
        Thread.sleep(10);
        t1.start();
        Thread.sleep(10);
        t.start();
    }
    public static void main(String[] args) throws InterruptedException {
        for (int j = 0; j <10 ; j++) {
            ThreadDemo2();
            System.out.println();
            Thread.sleep(2000);
        }
    }
    //有三个线程，线程名称分别为：a，b，c。
//每个线程打印自己的名称。需要让他们同时启动，并按 c，b，a的顺序打印
    public static void main1(String[] args) throws InterruptedException {
        Object locker=new Object();
        Object locker1=new Object();
        Thread t1=new Thread(()->{
            System.out.print("a"+" ");
            synchronized (locker) {
                locker.notify();
            }
        });
        Thread t2=new Thread(()->{
            try {
                synchronized (locker) {
                    locker.wait();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.print("b"+" ");
            synchronized (locker1) {
                locker1.notify();
            }
        });
        Thread t3=new Thread(()->{
            try {
                synchronized (locker1) {
                    locker1.wait();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.print("c");
        });
        t2.start();
        t3.start();
        Thread.sleep(30);
        t1.start();
    }
}
